define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    //修改题目
    //console.log("!!!!!!!!!!!!!");
    $(document).on("click",".publish",function(){
        var question_id = $("#question_id").val();
        console.log("---------------------------------------------------------::"+question_id)
        var course_id = $("#course_name option:selected").attr("data-id");
        var course_name = $("#course_name option:selected").val();
        var type = $("#category option:selected").attr("data-id");
        var title = $("#content").val();
        var txt = document.getElementsByName("txt").length;
        var sum = 0;
        //var options = "[";
        var options = []
        console.log("text:"+txt);
        //console.log("___________________________________");

        var inputs=$("input[name='txt']");
        console.log(inputs);
        $.each(inputs,function(i){
            console.log(this);
            //console.log(this);
            if($(this).next()[0].checked == true){
                var correct = 1;
                sum += 1;
            }else{
                var correct = 0;
            }
            options.push({content:$(this).val(), correct:correct, option_id:$(this).attr("data-id")});

        })
        //for(var i=0;i<txt;i++){
            //console.log("WWWWWCCCCCCCCCC");
            //var option = $("#txt"+i).val();
            //console.log("option"+option);
            //var answer = $('input:checkbox[id='+i+']:checked').attr("id");
            //console.log("answer"+answer);
            //
            //if(option!=undefined)
            //    options+= "{\"content\":\""+option+"\"";
            //if(answer&&i<txt-1){
            //    options+= ",\"correct\":1},";
            //    sum++;
            //}else if(answer&&i==txt-1){
            //    options+= ",\"correct\":1}]";
            //    sum++;
            //}else if(!answer&&i<txt-1){
            //    options+= ",\"correct\":0},";
            //}else{
            //    options+= ",\"correct\":0}]";
            //}

        //}
        console.log(options);
        console.log("Lalalala");
        console.log(JSON.stringify(options));
        options = JSON.stringify(options);
        var param = {};
        param.question_id = question_id;
        if(course_id){
            param.course_name = course_name;
            param.course_id = course_id;
        }else{
            alert("请选择题目所属课程~");
            return;
        }
        if(type){
            param.type = type;
        }else{
            alert("请选择题目类型~");
            return;
        }
        if(title){
            param.title = title;
        }else{
            alert("请输入题目详情~");
            return;
        }
        if(options==''){
            alert("请填写选项~");
            return;
        }else if(sum==0){
            alert("请选择正确选项~")
            return;
        }else{
            param.options = options;
        }
        if(sum>1&&type<=1){
            alert("答案数与题型不匹配~")
            return;
        }
        if(param){
            bootbox.confirm("确定要修改该题目？",function(result){
                if(result) {
                    IO.post("act/editquestion",param,function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/question';
                            }
                        })
                    });
                }
            });
        }
    });


});
